<?php

class Application_Form_Patient extends Zend_Form {

    public function __construct($formAction = "Enregistrer", $legende = 'Nouveau') {
        //parent::__construct($options);
        //}
        //public function init() {


        $this->setDecorators(array('FormElements', 'Form')); // supprime la balise <dl>


        $decorators = array(
            'ViewHelper',
            'Errors',
            array('Description', array('tag' => 'p', 'class' => 'description')),
            array(array('data' => 'HtmlTag'), array('tag' => 'div', 'class' => 'input')), // 3
            array('Label', array('tag' => null, 'class' => '')), // 2
            array(array('row' => 'HtmlTag'), array('tag' => 'div', 'class' => 'clearfix')) // 1) exterieur
        );

        //$bntDecorators = array('ViewHelper', array('HtmlTag', array('tag' => 'span')));

        $id = new Zend_Form_Element_Hidden('id_patient');
        $id->setBelongsTo('Patient')
                ->addFilter('Int')
                ->addFilter('Null')
                ->setDecorators(array('ViewHelper'));


        $id_adresse = new Zend_Form_Element_Hidden('id_adresse');
        $id_adresse->setBelongsTo('Patient')
                ->addFilter('Int')
                ->addFilter('Null')
                ->setDecorators(array('ViewHelper'));


        $title = new Zend_Form_Element_Select('titre');
        $title->setLabel('Civilité')
                ->setBelongsTo('Patient')
                ->setAttribs(array('class' => 'span2'))
                ->setMultiOptions(array('' => '', 'mr' => 'Monsieur', 'mme' => 'Madame'))
                ->setRequired(true)->addValidator('NotEmpty', true);
        //->setDecorators($decorators);

        $nom = new Zend_Form_Element_Text('nom');
        $nom->setLabel('Nom')
                ->setBelongsTo('Patient')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');

        $prenom = new Zend_Form_Element_Text('prenom');
        $prenom->setLabel('Prenom')
                ->setBelongsTo('Patient')
                ->setRequired(true)
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty');


        $adresse = new Zend_Form_Element_Text('adresse');
        $adresse->setlabel('Adresse')
                ->setBelongsTo('Adresse')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->setAttrib('class', 'xlarge')
                ->addValidator('NotEmpty');

        $adresse2 = new Zend_Form_Element_Text('adresse_2');
        $adresse2->setlabel('Adresse suite')
                ->setBelongsTo('Adresse')
                ->addFilter('StringTrim')
                ->setAttrib('class', 'xlarge');

        $codePostal = new Zend_Form_Element_Text('cpostal');
        $codePostal->setlabel('Code Postal')
                ->setBelongsTo('Adresse')
                ->setRequired(true)
                ->addValidator('regex', true, array('/^[0-9\s]{5}/'))
                ->addErrorMessage('Code Postal invalide !')
                //->addValidator('Digits', true)
                ->addFilter('StringTrim')
                ->setAttrib('class', 'span2')
                //->addValidator($validator, $breakChainOnFailure)
                ->addValidator('NotEmpty', true);

        $ville = new Zend_Form_Element_Text('ville');
        $ville->setlabel('Ville')
                ->setBelongsTo('Adresse')
                ->setRequired(true)
                ->addFilter('StringTrim')
                ->setAttrib('class', 'xlarge')
                ->addValidator('NotEmpty');

        $portable = new Zend_Form_Element_Text('portable');
        $portable->setlabel('Portable')
                ->setBelongsTo('Adresse')
                ->addValidator('regex', true, array('/^[0-9\s]{10,14}/'))
                ->addErrorMessage('Nr de portable invalide !')
                ->setRequired(true)
                //->addFilter('StringTrim')
                ->setAttrib('class', 'span3');

        $tel = new Zend_Form_Element_Text('tel');
        $tel->setlabel('Téléphone fixe')
                ->setBelongsTo('Adresse')
                ->addValidator('regex', true, array('/^[0-9\s]{10,14}/'))
                ->addErrorMessage('Nr de Téléphone invalide !')
                ->setRequired(false)
                //->addFilter('StringTrim')
                ->setAttrib('class', 'span3');


        $urlBack = $this->getView()->url(array('controller' => 'patient', 'action' => 'index'));
        $cancel = new Zend_Form_Element_Button('cancel');
        $cancel->setLabel('Annuler')
                ->setAttrib('class', 'btn')
                ->setAttrib('type', 'reset')
                ->setDecorators(array('ViewHelper'))
                ->setAttrib('onclick', 'window.location =\'' . $urlBack . '\' ');

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel($formAction)
                ->setAttrib('id', 'submitbutton')
                ->setAttrib('class', 'btn primary');
        //->setDecorators(array('ViewHelper'));


        $this->addElements(array($id, $id_adresse, $title, $nom, $prenom, $adresse, $adresse2, $ville, $codePostal, $portable, $tel, $cancel, $submit));
        $this->setElementDecorators($decorators, array('titre', 'nom', 'prenom', 'adresse', 'adresse_2', 'cpostal', 'ville', 'portable', 'tel'));
        $this->setElementDecorators(array('ViewHelper'), array('submit', 'cancel'));


        // Patient Group
        $this->addDisplayGroup(array(
            'titre', 'prenom', 'nom', 'adresse', 'adresse_2',
            'ville', 'cpostal', 'portable', 'tel'), 'patient', array('legend' => $legende . " Patient", 'style' => 'font-size:12px;'));

        $contact = $this->getDisplayGroup('patient');
        $contact->setDecorators(array(
            'FormElements',
            'Fieldset',
            array('HtmlTag', array('tag' => 'div', 'class' => 'span12', '_style' => 'width:40%;color:blue;')),
            array(array('rowspan' => 'HtmlTag'), array('tag' => 'div', 'class' => 'row'))));


        // Buttons Group
        $this->addDisplayGroup(array('cancel', 'submit'), 'buttonGroupe');
        $this->buttonGroupe->removeDecorator('Label');
        $this->buttonGroupe->setDecorators(array(
            'FormElements',
            array('HtmlTag', array('tag' => 'div', 'class' => 'actions'))
        ));
    }

}

